package com.easylinkin.linkappapi.openapi.service.impl;

import com.easylinkin.linkappapi.dashboard.entity.Dashboard;
import com.easylinkin.linkappapi.dashboard.service.DashboardService;
import com.easylinkin.linkappapi.openapi.service.ApplicationCopyScreenService;
import com.easylinkin.linkappapi.security.entity.LinkappUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * @program: linkapp-group-cloud
 * @description: 大屏地址
 * @author: chenkaixuan
 * @create: 2021-09-07 14:19
 */
@Slf4j
@Order(2)
@Service
public class ApplicationCopyScreenAddrServiceImpl implements ApplicationCopyScreenService {
    @Resource
    DashboardService dashboardService;

    @Override
    public RestMessage copy(HashMap<String,String> map,HashMap<String,String> deviceMap,LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        if (newLinkappUser == null || oldLinkappUser == null) {
            return RestBuilders.failureMessage().setMessage("用户为空");
        }
        RestMessage restMessage = RestBuilders.successBuilder().build();
//        if (map == null || map.size() <= 0) {
//            return restMessage;
//        }
        Dashboard dashboard = dashboardService.getDashboardByTenantId(oldLinkappUser.getTenantId());
        if(dashboard == null || StringUtils.isEmpty(dashboard.getDashboardUrl())){
            return restMessage;
        }
        if(map != null && map.get(dashboard.getDashboardUrl()) != null){
            dashboard.setDashboardUrl(map.get(dashboard.getDashboardUrl()));
        }
        dashboard.setId(null);
        dashboard.setTenantId(newLinkappUser.getTenantId());
        dashboard.setName(newLinkappUser.getUsername()+"的大屏"+RandomStringUtils.randomAlphanumeric(4));
        dashboardService.addDashboard(dashboard);
        log.info("大屏配置拷贝完成");
        return restMessage;
    }

    @Override
    public RestMessage copy(LinkappUser newLinkappUser, LinkappUser oldLinkappUser) {
        return null;
    }
}
